﻿using DomainService.IService;
using DomainService.IService.Sys;
using DomainService.Service.Sys;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;
using Model.BusinessModel.Input;
using Model.BusinessModel.OutPut;
using Model.BusinessModel.OutPut.sys;
using OverallAuth_WebApi.PlugInU;
using Utility;
using Utility.Enum;

namespace OverallAuth_WebApi.Controllers.OpenApi
{
    /// <summary>
    /// Sys_Button 按钮
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "开放接口")]
    public class OpenApiController : BaseController
    {

        #region 构造实列化

        /// <summary>
        /// 用户服务
        /// </summary>
        public ISysUserService _userService;

        /// <summary>
        /// 登录日志服务
        /// </summary>
        public ISysLoginLogService _sysLoginLogService;


        /// <summary>
        /// 菜单服务服务
        /// </summary>
        public ISysMenuService _sysMenuService;

        /// <summary>
        /// 公司服务
        /// </summary>
        public ISysCorporationService _sysCorporationService;

        /// <summary>
        /// 基础服务
        /// </summary>
        public IBaseService _baseService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userService">用户服务</param>
        /// <param name="sysLoginLogService">日志服务</param>
        /// <param name="sysMenuService">菜单服务</param>
        /// <param name="sysCorporationService">公司服务</param>
        /// <param name="baseService"></param>
        public OpenApiController(ISysUserService userService, ISysLoginLogService sysLoginLogService, ISysMenuService sysMenuService, ISysCorporationService sysCorporationService, IBaseService baseService)
        {
            _userService = userService;
            _sysLoginLogService = sysLoginLogService;
            _sysMenuService = sysMenuService;
            _sysCorporationService = sysCorporationService;
            _baseService= baseService;
        }

        #endregion

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous] // 不验证权限
        public ReceiveStatus<LoginOutPut> Login(LoginInput loginModel)
        {
            var result = _userService.GetUserMsg(loginModel.UserName, loginModel.Password, loginModel.CorporationKey, loginModel.SystemKey);
            if (result.success)
            {
                var loginResult = result.data.First();
                var tokenResult = JwtPlugIn.BuildToken(loginResult);
                loginResult.Token = tokenResult.Token;
                loginResult.ExpiresDate = tokenResult.ExpiresDate;
                result.data = new List<LoginOutPut>() { loginResult };
            }

            return result;
        }

        /// <summary>
        /// 获取所有公司数据,select结构，登录使用
        /// </summary>
        /// <returns>以select结构，返回所有公司</returns>
        [HttpGet]
        [AllowAnonymous]
        public ReceiveStatus<SelectOutPut> GetCorporation()
        {
            ReceiveStatus<SelectOutPut> receiveStatus = new();
            var list = _sysCorporationService.GetAll(string.Empty);
            receiveStatus.data = list;
            return receiveStatus;
        }

        /// <summary>
        /// 获取用户所属菜单
        /// </summary>
        /// <returns>返回用户所属菜单</returns>
        [HttpGet]
        public ReceiveStatus<SysMenuOutPut> GetMenuByUserId()
        {
            var loginModel = GetLoginUserMsg();
            return _sysMenuService.GetMenuByUserId(loginModel.UserId, loginModel.CorporationKey, Const.OverallAuth_SystemKey);
        }


        /// <summary>
        /// 获取用户系统菜单--第三方系统
        /// </summary>
        /// <param name="loginModel">登录模型</param>
        /// <returns></returns>
        [HttpPost]
        public ReceiveStatus<SysMenuOutPut> GetUserSystemMenu(LoginInput loginModel)
        {
            return _sysMenuService.GetMenuByUserId(loginModel.UserId, loginModel.CorporationKey, loginModel.SystemKey);
        }

        /// <summary>
        /// 获取权限条件
        /// </summary>
        /// <param name="ruleType">权限规则类型</param>
        /// <param name="mainTableAlias">主表别名</param>
        ///  <param name="menuId">菜单id</param>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus<string> GetAuthWhere(RuleTypeEnum  ruleType, string mainTableAlias, string? menuId = null) 
        {
            return _baseService.GetAuthWhere(ruleType, mainTableAlias, menuId);
        }

    }
}
